package com.wjcxf.utils;

import com.wjcxf.paint.PaintGrid;
import com.wjcxf.struct.PopStar;

import java.awt.*;
import java.awt.image.BufferedImage;

public class ImageUtil {
    //
//    public static final Color redStarColor = new Color(226,70,110);
//    public static final Color greenStarColor = new Color(102,202,26);
//    public static final Color blueStarColor = new Color(70,190,251);
//    public static final Color yellowStarColor = new Color(255,185,15);
//    public static final Color purpleStarColor = new Color(192,60,252);

    public static final Color redStarColor = new Color(226,70,110);
    public static final Color greenStarColor = new Color(102,202,26);
    public static final Color blueStarColor = new Color(70,190,251);
    public static final Color yellowStarColor = new Color(255,185,15);
    public static final Color purpleStarColor = new Color(192,60,252);
    public static final Color blackStarColor = new Color(1,1,1);
    //判断两个color是否相似
    public static boolean checkColorIsSimilar(Color color1,Color color2,int err){
        if(Math.abs(color1.getRed() - color2.getRed()) <= err && Math.abs(color1.getGreen() - color2.getGreen()) <= err && Math.abs(color1.getBlue() - color2.getBlue()) <= err){
            return true;
        }
        return false;
    }
    //根据定位网格和截取的图片返回数组
    public static int[][] getPopStarArrByImg(PaintGrid grid, BufferedImage bufferedImage,int imgX,int imgY,float scale){
        int[][] popStarArr = new int[PopStar.ROW][PopStar.COL];
        //允许误差
        int error = 10;
        for(int i= 0;i<PopStar.ROW;i++){
            for (int j= 0;j<PopStar.COL;j++){
                Point position = grid.getMobilePosition(i,j);
                int colorNumber = bufferedImage.getRGB(position.x,position.y);
                Color color = new Color(colorNumber);
                popStarArr[i][j] = 0;
                if(checkColorIsSimilar(color,redStarColor,error)){
                    popStarArr[i][j] = 1;
                    continue;
                }
                if(checkColorIsSimilar(color,greenStarColor,error)){
                    popStarArr[i][j] = 2;
                    continue;
                }
                if(checkColorIsSimilar(color,blueStarColor,error)){
                    popStarArr[i][j] = 3;
                    continue;
                }
                if(checkColorIsSimilar(color,yellowStarColor,error)){
                    popStarArr[i][j] = 4;
                    continue;
                }
                if(checkColorIsSimilar(color,purpleStarColor,error)){
                    popStarArr[i][j] = 5;
                    continue;
                }

            }
        }
        return popStarArr;
    }
    public static int identifyColor(Color color,int error){
        if(checkColorIsSimilar(color,redStarColor,error)){
            return 1;
        }
        if(checkColorIsSimilar(color,greenStarColor,error)){
            return 2;
        }
        if(checkColorIsSimilar(color,blueStarColor,error)){
            return 3;
        }
        if(checkColorIsSimilar(color,yellowStarColor,error)){
            return 4;
        }
        if(checkColorIsSimilar(color,purpleStarColor,error)){
            return 5;
        }
        return 0;
    }
    public static Color getColorByType(int type){
        Color color = blackStarColor;
        switch (type){
            case 1:
                color = redStarColor;
                break;
            case 2:
                color = greenStarColor;
                break;
            case 3:
                color = blueStarColor;
                break;
            case 4:
                color = yellowStarColor;
                break;
            case 5:
                color = purpleStarColor;
                break;
            default:
                break;
        }
        return color;
    }
}
